﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CSP.Web.Common
{
    public partial class SnsValidation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //MobileService ms = new MobileService();
            //ms.Host = "http://sms-api.zhangxin010.com";
            //ms.Port = 9982;
            //ms.Username = "leo";
            //ms.Password = "1";
            //String result = ms.sendGetRequest("45ff9134-2e2a-4cc2-b2a9-2a7489b2d479", "1", str, "test", 1, string.Empty, 1);
            ////Console.WriteLine(result);
            //result = ms.sendPostRequest("45ff9134-2e2a-4cc2-b2a9-2a7489b2d479", "1", str, "test", 1, string.Empty, 1);
        }
    }
    public class MobileService
    {
        private string host;
        public String Host { get { return host; } set { host = value; } }
        private int port;
        public int Port { get { return port; } set { port = value; } }
        private String username;
        public String Username { get { return username; } set { username = value; } }
        private String password;
        public String Password { get { return password; } set { password = value; } }
        public String sendGetRequest(String contractId, String msgId, String[] mobiles, String content, int encoding, String scheduleTime, int priority)
        {
            StringBuilder sb = new StringBuilder();
            StringBuilder url = new StringBuilder();
            url.Append(this.host).Append(":").Append(this.port).Append("/?commandId=0x00000001");
            url.Append("&username=").Append(this.username).Append("&password=").Append(this.password);
            url.Append("&contractId=").Append(contractId); url.Append("&msgId=").Append(msgId);
            StringBuilder mobile = new StringBuilder();
            foreach (String str in mobiles) { mobile.Append(str).Append(","); }
            mobile.Remove(mobile.Length - 1, 1); url.Append("&mobile=").Append(mobile.ToString());
            if (encoding == 1)
            {
                url.Append("&content=").Append(HttpUtility.UrlEncode(Convert.ToBase64String(Encoding.GetEncoding("gbk").GetBytes(content))));
            }
            else if (encoding == 2)
            {
                url.Append("&content=").Append(HttpUtility.UrlEncode(Convert.ToBase64String(Encoding.UTF8.GetBytes(content))));
            }
            url.Append("&encoding=").Append(encoding); url.Append("&scheduleTime=").Append(scheduleTime);
            url.Append("&priority=").Append(priority);
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.ToString());
                request.Method = "GET";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                sb.Append(retString);
            }
            catch (Exception e) { Console.WriteLine(e.StackTrace); Console.WriteLine(e.Message); } return sb.ToString();
        }

        public String sendPostRequest(String contractId, String msgId, String[] mobiles, String content, int encoding, String scheduleTime, int priority)
        {
            StringBuilder sb = new StringBuilder(); StringBuilder url = new StringBuilder(); url.Append("contractId=").Append(contractId); url.Append("&msgId=").Append(msgId); StringBuilder mobile = new StringBuilder();
            foreach (String str in mobiles)
            {
                mobile.Append(str).Append(",");
            }

            mobile.Remove(mobile.Length - 1, 1);
            url.Append("&mobile=").Append(mobile.ToString());
            if (encoding == 1)
            {
                url.Append("&content=").Append(HttpUtility.UrlEncode(Convert.ToBase64String(Encoding.GetEncoding("gbk").GetBytes(content))));
            }
            else if (encoding == 2)
            {
                url.Append("&content=").Append(HttpUtility.UrlEncode(Convert.ToBase64String(Encoding.UTF8.GetBytes(content))));
            }
            url.Append("&encoding=").Append(encoding);
            url.Append("&scheduleTime=").Append(scheduleTime);
            url.Append("&priority=").Append(priority);
            byte[] data = Encoding.Default.GetBytes(url.ToString());
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.host + ":" + this.port + "/");
                request.ServicePoint.Expect100Continue = false;
                request.Headers.Add("commandId", "0x00000001");
                request.Headers.Add("username", this.username);
                request.Headers.Add("password", this.password);
                request.Method = "POST"; request.Accept = "*/*";
                request.ContentLength = data.Length;
                request.ContentType = "application/x-www-form-urlencoded";
                request.KeepAlive = true;
                request.Timeout = 5000;
                Stream outStream = request.GetRequestStream();
                outStream.Write(data, 0, data.Length);
                outStream.Flush();
                outStream.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                sb.Append(retString);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
                Console.WriteLine(e.Message);
            }
            return sb.ToString();
        }
    }
}